//
//  HQHotViewController.m
//  MobileiOS
//
//  Created by 郝庆松 on 2018/5/4.
//  Copyright © 2018年 PCITC. All rights reserved.
//

#import "HQHotViewController.h"
#import "HotProductCollCell.h"
#import "HQHotTopView.h"
#import "HQShopDetailViewController.h"
#import "HQShopViewController.h"
#import "HQHotDetailViewController.h"
#import "HQNewProductDetailViewController.h"
#import "HQHotMoreDetailViewController.h"

@interface HQHotViewController ()<HQHotTopViewDelegate>

@end

@implementation HQHotViewController

- (void)viewDidLoad{
    [super viewDidLoad];
    
    self.navigationItem.title = @"热点";
    //    [self addSearchBar2:@"搜索产品风格与厂家"];
    
    // 下拉刷新
    _collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self requesList];
        [_collectionView.mj_header endRefreshing];
    }];
    [_collectionView.mj_header beginRefreshing];
    
    //    _collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
    //        [_collectionView.mj_footer endRefreshing];
    //    }];
    [_collectionView registerNib:[UINib nibWithNibName:@"HotProductCollCell" bundle:nil] forCellWithReuseIdentifier:@"HotProductCollCell"];
    [_collectionView registerNib:[UINib nibWithNibName:@"NewCollProductCell" bundle:nil] forCellWithReuseIdentifier:@"NewCollProductCell"];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if(section == 0){
        return 1;
    }else{
        return [_dataSource[section] count];
    }
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return _dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 0){
        HotProductCollCell *cell = [HotProductCollCell cellWithCollectionView:collectionView withIndexPath:indexPath];
        HQHotTopView * newsView = [HQHotTopView loadTopHotView];
        newsView.frame = cell.bounds;
        newsView.delegate = self;
        newsView.dataSource = _dataSource[indexPath.section];
        [cell addSubview:newsView];
        return cell;
    }else{
        NewCollProductCell *cell = [NewCollProductCell cellWithCollectionView:collectionView withIndexPath:indexPath];
        cell.dataInfo = _dataSource[indexPath.section][indexPath.row];
        return cell;
    }
}

/**
 热点样式详情

 @param hottopView 顶部热点视图
 @param dataInfo 数据包
 */
-(void)selectItem:(HQHotTopView *)hottopView withInfo:(NSDictionary *)dataInfo{
    HQHotMoreDetailViewController *hotMoreDetailVc = [[HQHotMoreDetailViewController alloc]init];
    hotMoreDetailVc.hidesBottomBarWhenPushed = YES;
    hotMoreDetailVc.dataInfo = dataInfo;
    [self.navigationController pushViewController:hotMoreDetailVc animated:YES];
}

// 新品详情
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    HQNewProductDetailViewController *productDetailVc = [[HQNewProductDetailViewController alloc]init];
    productDetailVc.hidesBottomBarWhenPushed = YES;
    productDetailVc.dataInfo =  _dataSource[indexPath.section][indexPath.row];
    productDetailVc.productListArray = _dataSource[indexPath.section];
    productDetailVc.currentIndex = indexPath.row;
    [self.navigationController pushViewController:productDetailVc animated:YES];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 0){
        return CGSizeMake(SCREEN_WIDTH, 200);
    }else{
        return CGSizeMake(SCREEN_WIDTH, 400);
    }
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(self.view.bounds.size.width, 40);
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        if(indexPath.section == 0){
            [collectionView registerNib:[UINib nibWithNibName:@"HQHotHeaderView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HQHotHeaderView"];
            UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind  withReuseIdentifier:@"HQHotHeaderView" forIndexPath:indexPath];
            UITapGestureRecognizer *ges = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hotStyleMore)];
            [view addGestureRecognizer:ges];
            return view;
        }else{
            [collectionView registerNib:[UINib nibWithNibName:@"HQHotHeader2View" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HQHotHeader2View"];
            UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind  withReuseIdentifier:@"HQHotHeader2View" forIndexPath:indexPath];
            UITapGestureRecognizer *ges = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(newProductMore)];
            [view addGestureRecognizer:ges];
            return view;
        }
        
    }
    return nil;
}

/**
 更多热点样式
 */
-(void)hotStyleMore{
    HQHotMoreViewController *h = [[HQHotMoreViewController alloc]init];
    h.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:h animated:YES];
}

/**
 更多新品
 */
-(void)newProductMore{
    HQNewProductMoreViewController *h = [[HQNewProductMoreViewController alloc]init];
    h.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:h animated:YES];
}

-(void)reloadItem:(NSString *)text{
    NSLog(@"打印 %@",text);
}

-(void)requesList{
    _collectionView.backgroundView = nil;
    [self showHudText:@""];
    [HQNetWork requetParam:@{} withMethod:[HQMethod hotsIndex] suc:^(NSDictionary *sucData) {
        [self hiddnHud];
        if([sucData[kStatus]integerValue] == 1){
            NSArray *n =sucData[@"data"][@"hot_styles"];
            NSArray *r =sucData[@"data"][@"hot_lists_xp"];
            [_dataSource removeAllObjects];
            _dataSource = [[NSMutableArray alloc]initWithArray:@[n,r]];
            [_collectionView reloadData];
        }else{
            [self tostView:sucData[kInfo]];
        }
    } fail:^{
        [self hiddenErrorHud];
    }];
}

@end
